package com.leon.androidcoursedesign.ui.fragments.noteFragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.navigation.Navigation;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.beans.Note;
import com.leon.androidcoursedesign.ui.dialogs.NoteDialog;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BaseFragment;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BindingUtils;
import com.leon.androidcoursedesign.utils.ValueUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.NoteUtils;

import java.util.UUID;

public class NoteEditFragment extends BaseFragment implements View.OnClickListener, NoteDialog.OnCallback {
    private Note note;
    private NoteDialog noteDialog;
    private EditText note_edit_fragment_editText;
    private TextView note_edit_fragment_title;

    private NoteUtils noteUtils;
    private String operationId;

    @Override
    public int setLayout() {
        return R.layout.note_edit_fragment;
    }

    @Override
    public void init() {
        operationId = ValueUtils.getLoginId(context);

        Bundle arguments = getArguments();
        if (arguments != null) {
            note = (Note) arguments.getSerializable("note");
        } else {
            note = new Note();
            note.operationId = operationId;
            note.itemId = ValueUtils.getRandomId();
            note.noteId = UUID.randomUUID().toString();
        }
    }

    @Override
    public void initView(BindingUtils bindingUtils) {
        bindingUtils
                .setOnClickListener(R.id.note_edit_fragment_imageView_back, this)
                .setOnClickListener(R.id.note_edit_fragment_imageView_save_note, this)
                .setOnClickListener(R.id.note_edit_fragment_title, this);

        note_edit_fragment_editText = findView(R.id.note_edit_fragment_editText);
        note_edit_fragment_title = findView(R.id.note_edit_fragment_title);
    }

    @Override
    public void initValues() {
        noteUtils = new NoteUtils(context);

        if (note.noteId != null) {
            String content = ValueUtils.readNote(context, note.noteId);
            note_edit_fragment_editText.setText(content);
            note_edit_fragment_title.setText(note.showName);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.note_edit_fragment_title:
                if (note.showName == null) {
                    noteDialog = new NoteDialog(context, "添加笔记标题", false);
                } else {
                    noteDialog = new NoteDialog(context, "修改笔记标题", true);
                }

                noteDialog.setOnCallback(this);
                noteDialog.show();

                break;
            case R.id.note_edit_fragment_imageView_save_note:
                // 判断笔记标题是否为null
                if (note.showName == null) {
                    noteDialog = new NoteDialog(context, "添加笔记标题", false);
                    noteDialog.setOnCallback(this);
                    noteDialog.show();
                }
                note.saveTime = System.currentTimeMillis();

                if (noteDialog == null || !noteDialog.isShowing()) {
                    saveNote();
                }

                break;
            case R.id.note_edit_fragment_imageView_back:
                // 隐藏软键盘
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

                Navigation.findNavController(view).navigate(R.id.action_noteEditFragment_to_noteListFragment);
                break;
            default:
                break;
        }
    }

    @Override
    public void callBackTitle(String noteTitle) {
        note.showName = noteTitle;
        note_edit_fragment_title.setText(note.showName);
        noteUtils.updateNote(note);
    }

    @Override
    public void saveNote() {
        if (noteUtils.queryNote(note.noteId, operationId)) {
            noteUtils.updateNote(note);
        } else {
            if (note.saveTime == 0) {
                note.saveTime = System.currentTimeMillis();
            }
            noteUtils.addNote(note);
        }

        // 保存文件
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean saveState = ValueUtils.saveNote(context, note_edit_fragment_editText.getText().toString(), note.noteId);

                Looper.prepare();
                Toast.makeText(context, saveState ? "保存成功" : "保存失败", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }).start();
    }

    @Override
    public void onDestroy() {
        noteUtils.close();
        super.onDestroy();
    }
}
